package com.hethongviet.preventing.page;

import java.util.List;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

import com.hethongviet.preventingmachine.AppMain;
import com.hethongviet.preventingmachine.Config;
import com.hethongviet.preventingmachine.Constants;
import com.hethongviet.preventingmachine.R;
import com.hethongviet.preventingmachine.Utils;
import com.hethongviet.preventingmachine.helps.CommonUtils;
import com.hethongviet.preventingmachine.helps.Device;
import com.hethongviet.preventingmachine.helps.DeviceSQLite;
import com.hethongviet.preventingmachine.helps.dialog.DialogMessageBack;
import com.hethongviet.preventingmachine.helps.OnSMSMessageListener;
import com.hethongviet.preventingmachine.helps.SMSHelperDoor;
import com.hethongviet.preventingmachine.helps.SmsReceiverDoor;
import com.hethongviet.preventingmachine.models.SMS;

/** Page login */
public class PageLoginActivity extends Activity implements OnSMSMessageListener
{
	/**Database*/
	private DeviceSQLite _DeviceSQLite = null;
	private Device _Device = null;

	private EditText _EditTextDeviceNumber = null;
	private EditText _EditTextPassword = null;

	private Button _ButtonLogin = null;

	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);

		Utils.prepareWindow(this);

		setContentView(R.layout.page_login);

		_EditTextDeviceNumber = (EditText) findViewById(R.id.login_text_device_number);
		_EditTextPassword = (EditText) findViewById(R.id.login_text_password);

		_ButtonLogin = (Button) findViewById(R.id.login_button_login);

		initDB();

		if (TextUtils.isEmpty(_Device.getName()))
		{
			_Device.setName("VSYS");
		}

		_EditTextPassword.setText(_Device.getPin());
		_EditTextDeviceNumber.setText(_Device.getNumber());

		SMSHelperDoor.setDeviceNumber(_Device.getNumber());

		_ButtonLogin.setOnClickListener(new OnClickListener()
		{

			@Override
			public void onClick(View arg0)
			{
				String password = _EditTextPassword.getText().toString();

				if (password == null || password.equals(""))
				{
					try
					{
						showDialog(R.string.message_error_login_must_input_password);
					} catch (Exception e)
					{
						e.printStackTrace();
					}

					return;
				}
				for (int index = 0; index < password.length(); index++)
				{
					char c = password.charAt(index);

					if (!Character.isLetterOrDigit(c))
					{
						showDialogWithCatch(R.string.message_error_wrong_input_password);
						return;
					}
				}

				if (password.length() < Constants.LENGTH_MIN_PASSWORD)
				{
					showDialogWithCatch(R.string.message_error_input_too_short_password);
					return;
				}

				if (password.length() > Constants.LENGTH_MAX_PASSWORD)
				{
					showDialogWithCatch(R.string.message_error_input_too_long_password);
					return;
				}

				String number = _EditTextDeviceNumber.getText().toString();

				if (number == null || number.equals(""))
				{
					try
					{
						showDialog(R.string.message_error_login_must_input_number);
					} catch (Exception e)
					{
						e.printStackTrace();
					}
					return;
				}

				if (number.length() < Constants.LENGTH_MIN_NUMBER)
				{
					showDialogWithCatch(R.string.message_error_input_too_short_number);
					return;
				}

				if (number.length() > Constants.LENGTH_MAX_NUMBER)
				{
					showDialogWithCatch(R.string.message_error_input_too_long_number);
					return;
				}

				for (int index = 0; index < number.length(); index++)
				{
					char c = number.charAt(index);

					if (!Character.isDigit(c) && c != '+')
					{
						showDialogWithCatch(R.string.message_error_wrong_input_number);
						return;
					}
				}

				Utils.debug("SMSHelper.setDeviceNumber: " + number);
				_Device.setNumber(number);
				_Device.setPin(password);
				_DeviceSQLite.insert(_Device);

				/* Set SMS number */
				SMSHelperDoor.setDeviceNumber(number);

				/* Set SMS pin */
				SMSHelperDoor.setPassword(password);

				/* Update all */
				Utils.showProgress(PageLoginActivity.this);

				Utils.debug("Login successful!!!!!!");
				if (!Config.IS_DEBUG_SMS_RECEIVE)
				{
					SMSHelperDoor.updateNumberControl();
				}
				else
				{
					Intent intent = new Intent(PageLoginActivity.this,
							PageAdminActivity.class);
					intent.putExtra("numbers",
							"123123,324324,131231423,13213123#");
					startActivity(intent);
				}
			}
		});

	}

	@Override
	protected void onResume()
	{

		SmsReceiverDoor.setOnSMSMessageListener(this);
		SMSHelperDoor.setActivity(this);

		super.onResume();
	}

	private void showDialogWithCatch(int id)
	{
		try
		{
			showDialog(id);
		} catch (Exception e)
		{
			e.printStackTrace();
		}
	}

	private void initDB()
	{
		_DeviceSQLite = new DeviceSQLite(this,
				AppMain.getInstance().getDBName());
		List<Device> devices = _DeviceSQLite.getAll();
		if (devices != null && devices.size() != 0)
		{
			_Device = _DeviceSQLite.getAll().get(0);
		}
		else
		{
			_Device = new Device();
			_Device.setName("Door");
			_DeviceSQLite.insert(_Device);
			_Device = _DeviceSQLite.getAll().get(0);
		}
	}

	@Override
	protected Dialog onCreateDialog(int id)
	{
		if (id == R.string.message_error_input_too_long_number)
		{
			return new DialogMessageBack(this, true, String.format(
					getString(id), Constants.LENGTH_MAX_NUMBER), false);
		}
		else if (id == R.string.message_error_input_too_short_number)
		{
			return new DialogMessageBack(this, true, String.format(
					getString(id), Constants.LENGTH_MIN_NUMBER), false);
		}
		else if (id == R.string.message_error_input_too_long_password)
		{
			return new DialogMessageBack(this, true, String.format(
					getString(id), Constants.LENGTH_MAX_PASSWORD), false);
		}
		else if (id == R.string.message_error_input_too_short_password)
		{
			return new DialogMessageBack(this, true, String.format(
					getString(id), Constants.LENGTH_MIN_PASSWORD), false);
		}
		else
			return new DialogMessageBack(this, true, id, false);
	}

	@Override
	public void onSMSMessageIncoming(SMS sms)
	{

		Utils.debug("Message receive = " + sms.getMessage());
		String message = CommonUtils.prepareMessage(sms.getMessage());

		Utils.debug("Message receive valid = " + message);

		// if (message == null || message.equals(""))
		// {
		// Utils.debug("ERROR----- wrong message! NULL");
		// return;
		// }

		Utils.hideProgressDialog();

		Utils.debug("Login successful!!!!!!");

		Intent intent = new Intent(PageLoginActivity.this,
				PageAdminActivity.class);
		intent.putExtra("numbers", message);
		startActivity(intent);

	}
}
